﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data; 
using System.Data.Common;
using WMSADBUtility;
using WMSAML.SY;

namespace WMSADL.SY
{
    public class TUserPermissionRelation_DAL
    {
        /// <summary>
        /// 
        /// </summary>
        public int Add(TUserPermissionRelation_Entity entity)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into TUserPermissionRelation(");
            strSql.Append("User_id,Permission_id)");

            strSql.Append(" values (");
            strSql.Append("@User_id,@Permission_id)");
            strSql.Append(";select @@IDENTITY");
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "User_id", DbType.Int32, entity.User_id);
            db.AddInParameter(dbCommand, "Permission_id", DbType.String, entity.Permission_id);
            int result;
            object obj = db.ExecuteScalar(dbCommand);
            if (!int.TryParse(obj.ToString(), out result))
            {
                return 0;
            }
            return result;
        }

        /// <summary>
        /// ByUserID
        /// </summary>
        public bool Delete(int uid)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("DELETE FROM TUserPermissionRelation ");
            strSql.Append("WHERE User_id=@User_id ");
            Database db = DatabaseFactory.CreateDatabase();
            using (DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString()))
            {
                db.AddInParameter(dbCommand, "User_id", DbType.Int32, uid);
                int rows = db.ExecuteNonQuery(dbCommand);

                if (rows > 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(TUserPermissionRelation_Entity entity)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("UPDATE TUserPermissionRelation SET ");
            strSql.Append("User_id=@User_id,");
            strSql.Append("Permission_id=@Permission_id ");
            strSql.Append("WHERE UserPermission_id=@UserPermission_id ");
            Database db = DatabaseFactory.CreateDatabase();
            using (DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString()))
            {
                db.AddInParameter(dbCommand, "UserPermission_id", DbType.Int32, entity.UserPermission_id);
                db.AddInParameter(dbCommand, "User_id", DbType.Int32, entity.User_id);
                db.AddInParameter(dbCommand, "Permission_id", DbType.String, entity.Permission_id);
                int rows = db.ExecuteNonQuery(dbCommand);

                if (rows > 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }

        /// <summary>
        /// ByUserID
        /// </summary>
        public bool ExistsByID(int id)
        {
            Database db = DatabaseFactory.CreateDatabase();
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT COUNT(1) FROM TUserPermissionRelation WHERE User_id=@User_id ");
            using (DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString()))
            {
                db.AddInParameter(dbCommand, "User_id", DbType.Int32, id);
                int cmdresult;
                object obj = db.ExecuteScalar(dbCommand);
                if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
                {
                    cmdresult = 0;
                }
                else
                {
                    cmdresult = int.Parse(obj.ToString());
                }
                if (cmdresult == 0)
                {
                    return false;
                }
                else
                {
                    return true;
                }
            }
        }

        /// <summary>
        /// ByID
        /// </summary>
        /// <summary>
        public TUserPermissionRelation_Entity GetEntityByID(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT UserPermission_id,User_id,Permission_id  FROM TUserPermissionRelation ");
            strSql.Append(" WHERE UserPermission_id=@UserPermission_id ");
            Database db = DatabaseFactory.CreateDatabase();
            using (DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString()))
            {
                db.AddInParameter(dbCommand, "UserPermission_id", DbType.String, id);
                TUserPermissionRelation_Entity entity = new TUserPermissionRelation_Entity();
                using (IDataReader dataReader = db.ExecuteReader(dbCommand))
                {
                    if (dataReader.Read())
                    {
                        entity.UserPermission_id = Convert.ToInt32(dataReader["UserPermission_id"].ToString());
                        entity.User_id = Convert.ToInt32(dataReader["User_id"].ToString());
                        entity.Permission_id = dataReader["Permission_id"].ToString();
                    }
                }
                return entity;
            }
        }
    }
}
